//
//  HZGoodsDetailVCData.m
//  app
//
//  Created by 刘华舟 on 15/5/26.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZGoodsDetailVCData.h"
#import "NSDictionary+SafetyGet.h"

static const NSInteger kPageSize = 15;

@implementation HZGoodsDetailVCData

-(id)initWithJSONData:(NSDictionary*)data{
    if (self = [super init]) {
        
        self.userInfo = [data dictionaryForKey:APIKEY_mall_user_info];
        NSNumber *productsNum = [[NSUserDefaults standardUserDefaults] objectForKey:kHZUserProductNumInCarts];
        self.cart = [NSString stringWithFormat:@"%d", [productsNum integerValue]];
        // offset =本地时间 - 服务器时间
        self.timeoffset = [[[SNBaseInfoManager shareManager] baseInfoObjectForKey:kHZTimeOffsetBetweenServerAndLocal] floatValue];
        
        self.headModel = [HZGoodsDetailHeadModel modelWithDictionary:data];
        
        self.pinglun = [data dictForKey:APIKEY_mall_pinlun];
        
        NSMutableArray *commentsArray = [NSMutableArray array];
        for (NSDictionary *dict in [data arrayForKey:APIKEY_mall_pinlun]) {
            HZGoodsCommentModel *model = [HZGoodsCommentModel modelWithDictionary:dict];
            [commentsArray addObject:model];
        }
        self.pinlunArray = commentsArray;
        self.noMoreData = NO;

        
        self.lastObjectIndex = self.pinlunArray.count - 1;

    
        self.customerSvrs = [data arrayForKey:APIKEY_mall_CustomerServices];
        if (self.customerSvrs.count) {
            NSDictionary *svrsDict = [self.customerSvrs firstObject];
            self.userID = [svrsDict stringForKey:APIKEY_mall_uid nilValue:@"0"];
            self.nickname = [svrsDict stringForKey:APIKEY_mall_nickname nilValue:@""];
            self.faceURL = [svrsDict stringForKey:APIKEY_mall_face nilValue:@""];
        }else{
            self.userID = @"0";
            self.nickname = @"";
            self.faceURL = @"";
        }
        
        _defaultBarTag = 0;
    }
    
    return self;
}

- (BOOL)addMoreDataWithArray:(NSArray *)moreDatas{
    if (moreDatas.count <= 0) {
        self.noMoreData = YES;
        return NO;
    }else{
        if (moreDatas.count < kPageSize) {
            self.noMoreData = YES;
        }
        
        NSMutableArray *totalArray = [NSMutableArray arrayWithArray:self.pinlunArray];
        for (NSDictionary *dict in moreDatas) {
            HZGoodsCommentModel *model = [HZGoodsCommentModel modelWithDictionary:dict];
            [totalArray addObject:model];
        };
        self.pinlunArray = totalArray;
        
        self.lastObjectIndex = self.pinlunArray.count - 1;
    }
    return YES;
}


@end
